PrimeFaces.locales['pt'] = {
    closeText: 'Fechar',
    prevText: 'Anterior',
    nextText: 'Próximo',
    currentText: 'Começo',
    monthNames: ['Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro'],
    monthNamesShort: ['Jan', 'Fev', 'Mar', 'Abr', 'Mai', 'Jun', 'Jul', 'Ago', 'Set', 'Out', 'Nov', 'Dez'],
    dayNames: ['Domingo', 'Segunda', 'Terça', 'Quarta', 'Quinta', 'Sexta', 'Sábado'],
    dayNamesShort: ['Dom', 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sáb'],
    dayNamesMin: ['D', 'S', 'T', 'Q', 'Q', 'S', 'S'],
    weekHeader: 'Semana',
    firstDay: 1,
    isRTL: false,
    showMonthAfterYear: false,
    yearSuffix: '',
    timeOnlyTitle: 'Só Horas',
    timeText: 'Tempo',
    hourText: 'Hora',
    minuteText: 'Minuto',
    secondText: 'Segundo',
    currentText: 'Data Atual',
    ampm: false,
    month: 'Mês',
    week: 'Semana',
    day: 'Dia',
    allDayText: 'Todo Dia'
};

jQuery.fn.removeNot = function(settings) {
    var $this = jQuery(this);
    var defaults = {
        pattern: /[^0-9]/,
        replacement: ''
    }
    settings = jQuery.extend(defaults, settings);

    $this.keyup(function() {
        var new_value = $this.val().replace(settings.pattern, settings.replacement);
        $this.val(new_value);
    });
    return $this;
}

function currencyFormat(objTextBox, e) {
    var SeparadorMilesimo = '.';
    var SeparadorDecimal = ',';
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';

    var whichCode;
    if (window.event) {
        whichCode = event.keyCode;
    } else {
        if (e.which) {
            whichCode = e.which
        }
        if (e.keyCode) {
            return true;
        }
    }
    key = String.fromCharCode(whichCode); // Valor para o código da Chave
    if (strCheck.indexOf(key) == -1)
        return false; // Chave inválida
    len = objTextBox.value.length;
    tam = objTextBox.size;
    if (len >= tam - 1)
        return false;
    for (i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0')
            && (objTextBox.value.charAt(i) != SeparadorDecimal))
            break;
    aux = '';
    for (; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i)) != -1)
            aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0)
        objTextBox.value = '';
    if (len == 1)
        objTextBox.value = '0' + SeparadorDecimal + '0' + aux;
    if (len == 2)
        objTextBox.value = '0' + SeparadorDecimal + aux;
    if (len > 2) {
        aux2 = '';
        for (j = 0, i = len - 3; i >= 0; i--) {
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
            objTextBox.value += aux2.charAt(i);
        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
    }
    return false;
}

function somenteNumero(e) {
    var tecla = (window.event) ? event.keyCode : e.which;
    if ((tecla > 47 && tecla < 58) || (tecla > 95 && tecla < 106))
        return true;
    else {
        if (tecla === 8 || tecla === 13)
            return true;
        else
            return false;
    }
}

function somenteLetra(e) {
    var tecla = (window.event) ? event.keyCode : e.which;
    if ((tecla > 64 && tecla < 91) || (tecla > 96 && tecla < 123))
        return true;
    else {
        if (tecla === 8 || tecla === 0)
            return true;
        else
            return false;
    }
}

function numeroLetra(e) {
    var numero = somenteNumero(e);
    var letra = somenteLetra(e);
    return (numero || letra);
}

function numeroEndreco(e) {
    var tecla = (window.event) ? event.keyCode : e.which;
    var numero = somenteNumero(e);
    var sn = (tecla === 83 || tecla === 115 || tecla === 47 || tecla === 78 || tecla === 110);
    return (numero || sn);
}

function redimensionar(id) {
    var t = document.getElementById(id);
    var height = document.body.clientHeight;
    t.style.height = ((height - 260) + "px");
}

function formatarCodigoImovel(codigo, limite) {
    var length = codigo.length;
    if (limite !== null && length > 0) {
        var diff = limite - length;
        var zero = "";
        var i = 0;
        if (diff > 0) {
            for (i = 0; i < diff; i++) {
                zero = zero + "0";
            }
        }
        return (zero + codigo);
    } else {
        return codigo;
    }
}

function scrollToTop(idElement) {
    jQuery('#' + idElement).animate({
        scrollTop: 0
    }, 'slow');
}

function validarCampoData(valor) {
    if (valor === '__/__/____') {
        return '';
    } else {
        return valor;
    }
}

function mascarar(element, mask) {
    jQuery(element).mask(mask);
}

function maskMoney(o) {
    v_obj = o;
    v_fun = valor;
    setTimeout("execmascara()", 1);
}

function execmascara() {
    v_obj.value = v_fun(v_obj.value);
}

function valor(v) {
    v = v.replace(/\D/g, "");
    v = v.replace(/[0-9]{15}/, "");
    v = v.replace(/(\d{1})(\d{11})$/, "$1.$2"); // coloca ponto antes dos Últimos 11 digitos  
    v = v.replace(/(\d{1})(\d{8})$/, "$1.$2"); // coloca ponto antes dos Últimos 8 digitos  
    v = v.replace(/(\d{1})(\d{5})$/, "$1.$2"); // coloca ponto antes dos Últimos 5 digitos  
    v = v.replace(/(\d{1})(\d{1,2})$/, "$1,$2"); // coloca virgula antes dos Últimos 2 digitos
    return v;
}

function limitaQtdCaracteres(element, size) {
    return element.value.length <= size;
}

function doubleMask(element, limit) {
    var id = element.getAttribute("id");
    var idReplace = id.replace(":", "\\:", "g");
    jQuery('#' + idReplace).maskMoney({
        decimal: ",", 
        thousands: ".", 
        precision: limit
    });
}

function mascaraDouble(element, limit) {
    jQuery(element).maskMoney({
        decimal: ",", 
        thousands: ".", 
        precision: limit
    });
}

function codigoXplan(element) {

    element.value = element.value.toUpperCase();
    element.value = element.value.replace(/[-[\]{}.()~*+?%&amp;amp;amp;amp;amp;@!?¨_:;'"<>/=\\^$|#\b]^[ \t]+|[ \t]+$/g, "");
    var tamanho = element.value.length;
    var maxLenght = 7;

    if (tamanho >= 6 && !element.value.charAt(5).match(/[0-9]/)) {
        maxLenght = 8;
    }

    element.maxLength = maxLenght;

    if (tamanho < 6) {

        element.value = element.value.replace(/[0-9]{1}/, "");

    } else if (tamanho === 7 && element.value.charAt(5).match(/[0-9]/)) {

        if (!element.value.charAt(6).match(/[0-9]/)) {
            element.value = element.value.substring(0, 6);
        }

    } else if (tamanho >= 7 && !element.value.charAt(5).match(/[0-9]/)) {

        if (!element.value.charAt(6).match(/[0-9]/)) {
            element.value = element.value.substring(0, 6);
        }

        if (!element.value.charAt(7).match(/[0-9]/)) {
            element.value = element.value.substring(0, 7);
        }

    }

    if (tamanho > maxLenght) {
        element.value = element.value.substring(0, maxLenght);
    }

}

function formatarCodigoXplan(element) {

    element.value = element.value.toUpperCase();
    var length = element.value.length;

    if (length < 7) {
        element.value = '';
        return;
    } else if (!element.value.charAt(5).match(/[0-9]/) && length < 8) {
        element.value = '';
        return;
    } else if (element.value.contains("-")) {
        return;
    }

    if (length === 7) {
        element.value = element.value.replace(element.value.substring(2, 5), element.value.substring(2, 5) + "-");
        element.value = element.value.replace(element.value.substring(1, 2), element.value.substring(1, 2) + "-");
        element.value = element.value.replace(element.value.substring(0, 1), element.value.substring(0, 1) + "-");
    } else {
        element.value = element.value.replace(element.value.substring(3, 6), element.value.substring(3, 6) + "-");
        element.value = element.value.replace(element.value.substring(2, 3), element.value.substring(2, 3) + "-");
        element.value = element.value.replace(element.value.substring(0, 2), element.value.substring(0, 2) + "-");
    }

}

function removerFormatoXplan(element) {
    while (element.value.contains("-")) {
        element.value = element.value.replace(/[-]/, '');
    }
}

function blurSize(element) {

    var value = element.value.replace(/[-[\]{}.()~*+?%&amp;amp;amp;amp;amp;@!?¨_:;'"<>/=\\^$|#\b]^[ \t]+|[ \t]+$/g, "");
    var length = value.length;
    var maxLength = element.maxLength;

    if (length === maxLength) {
        jQuery.emulateTab();
    }

}

function upperCase(element) {
    element.value = element.value.toUpperCase();
}

function closeOnEscape(event) {
   
    var closeable;
    var indexHighest = 0;  
    var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
   
    if(keyCode !== 27){
        return;
    }
                                     
    jQuery(".ui-dialog.ui-widget.ui-widget-content.ui-overlay-visible").each(function() {
      
        var indexCurrent = parseInt($(this).css("zIndex"), 10);
                  
        if (indexCurrent > indexHighest) {
            indexHighest = indexCurrent;
            closeable = this;
        }
                   
    });   
   
    if (closeable !== null) {
      
        jQuery(closeable).removeClass('ui-overlay-visible').addClass('ui-overlay-hidden');
        jQuery(closeable).css({
            'visibility':'', 
            'z-index':'', 
            'display':''
        });
                    
        var modal = '#' + jQuery(closeable).attr('id') + '_modal';
                    
        jQuery(modal).remove();
      
    }
   
}

function obterIdUnico() {
                    
    return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
    s4() + '-' + s4() + s4() + s4();
                    
}                
                
function s4() {
                    
    return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
                    
}

function formatFloat(num,casasDec,sepDecimal,sepMilhar) {
    if (num < 0)
    {
        num = -num;
        sinal = -1;
    } else
        sinal = 1;
    var resposta = "";
    var part = "";
    if (num != Math.floor(num)) // decimal values present
    {
        part = Math.round((num-Math.floor(num))*Math.pow(10,casasDec)).toString(); // transforms decimal part into integer (rounded)
        while (part.length < casasDec)
            part = '0'+part;
        if (casasDec > 0)
        {
            resposta = sepDecimal+part;
            num = Math.floor(num);
        } else
            num = Math.round(num);
    } // end of decimal part
    while (num > 0) // integer part
    {
        part = (num - Math.floor(num/1000)*1000).toString(); // part = three less significant digits
        num = Math.floor(num/1000);
        if (num > 0)
            while (part.length < 3) // 123.023.123  if sepMilhar = '.'
                part = '0'+part; // 023
        resposta = part+resposta;
        if (num > 0)
            resposta = sepMilhar+resposta;
    }
    if (sinal < 0)
        resposta = '-'+resposta;
    return resposta;
}

function exibirMenuItem(idMenuItem, posicaoX, posicaoY) {
                    
    jQuery(PrimeFaces.escapeClientId(idMenuItem)).css({
        top: posicaoY,
        left: posicaoX
    }).show();                    
                    
}

function ocultarMenuItem(idMenuItem, posicaoX, posicaoY) {
                    
    jQuery(PrimeFaces.escapeClientId(idMenuItem)).css({
        top: posicaoY,
        left: posicaoX
    }).hide();                    
                    
}                

                
